﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SEC_TOOLBAR
// * CreateDate    : 2011/8/4 15:00:20
// * Creater       : CodeSmith
// * LastChangeDate: 2011/8/4 15:00:20
// * LastChanger   : 2011/8/4 15:00:20
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sec
{
    /// <summary>
    /// SecToolbar
    /// </summary>
    public partial class SecToolbarBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSecToolbarEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SecToolbarEntity GetSecToolbar(SecToolbarEntity objSecToolbarEntity)
        {
            SecToolbarEntity entity = DataAccess.SelectSingle<SecToolbarEntity>(objSecToolbarEntity);
            if (entity != null)
            {
                entity.MainMenuEntity = SecMenuBs.GetMenuByID(entity.MainMenu);
                entity.ToolbarMenuEntity = SecMenuBs.GetMenuByID(entity.ToolbarMenu);
            }
            return entity;
        }
        public static SecToolbarEntity GetSecToolbarByID(string mainMenuID, string toolbarMenuID)
        {
            if (string.IsNullOrEmpty(mainMenuID) || string.IsNullOrEmpty(toolbarMenuID))
            {
                return null;
            }
            SecToolbarEntity objSecToolbarEntity = new SecToolbarEntity();
            objSecToolbarEntity.MainMenu = mainMenuID;
            objSecToolbarEntity.ToolbarMenu = toolbarMenuID;
            SecToolbarEntity entity = DataAccess.SelectSingle<SecToolbarEntity>(objSecToolbarEntity);
            if (entity != null)
            {
                entity.MainMenuEntity = SecMenuBs.GetMenuByID(entity.MainMenu);
                entity.ToolbarMenuEntity = SecMenuBs.GetMenuByID(entity.ToolbarMenu);
            }
            return entity;
        }
        public static List<SecToolbarEntity> GetSecToolbarByMainMenu(string mainMenuID)
        {
            if (string.IsNullOrEmpty(mainMenuID))
            {
                return null;
            }
            string sql = @"SELECT ST.*
                            FROM SEC_TOOLBAR ST
                            INNER JOIN SEC_MENU SM ON SM.MENU_ID = ST.MAIN_MENU
                            WHERE MAIN_MENU = '" + mainMenuID + @"' 
                            ORDER BY SM.ORDER_INDEX";
            List<SecToolbarEntity> list = DataAccess.Select<SecToolbarEntity>(sql);
            if (list != null && list.Count > 0)
            {
                foreach (SecToolbarEntity entity in list)
                {
                    if (entity != null)
                    {
                        entity.MainMenuEntity = SecMenuBs.GetMenuByID(entity.MainMenu);
                        entity.ToolbarMenuEntity = SecMenuBs.GetMenuByID(entity.ToolbarMenu);
                    }
                }
            }
            return list;
        }

        public static List<SecToolbarEntity> GetSecToolbarByMainMenu(string mainMenuID, string userID)
        {
            if (string.IsNullOrEmpty(mainMenuID))
            {
                return null;
            }
            string sql = @"SELECT ST.*
                            FROM SEC_TOOLBAR ST
                            INNER JOIN SEC_MENU SM ON SM.MENU_ID = ST.MAIN_MENU
							INNER JOIN SEC_ROLE_MENU RM ON RM.MENU_ID = SM.MENU_ID
							INNER JOIN SEC_ROLE R ON R.ROLE_ID = RM.ROLE_ID
							INNER JOIN SEC_USER_ROLE UR ON UR.ROLE_ID = R.ROLE_ID
                            WHERE MAIN_MENU = '" + mainMenuID + @"' and UR.USER_ID = '" + userID + @"'
                            ORDER BY SM.ORDER_INDEX ";
            List<SecToolbarEntity> list = DataAccess.Select<SecToolbarEntity>(sql);
            if (list != null && list.Count > 0)
            {
                foreach (SecToolbarEntity entity in list)
                {
                    if (entity != null)
                    {
                        entity.MainMenuEntity = SecMenuBs.GetMenuByID(entity.MainMenu);
                        entity.ToolbarMenuEntity = SecMenuBs.GetMenuByID(entity.ToolbarMenu);
                    }
                }
            }
            return list;
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSecToolbarEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSecToolbar(SecToolbarEntity objSecToolbarEntity)
        {
            return DataAccess.Insert(objSecToolbarEntity);
        }

        public static bool InsertSecToolbar(List<SecToolbarEntity> objSecToolbarEntityList)
        {
            return DataAccess.Insert(objSecToolbarEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSecToolbarEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSecToolbar(SecToolbarEntity objSecToolbarEntity)
        {
            return DataAccess.Update(objSecToolbarEntity);
        }

        public static bool UpdateSecToolbar(List<SecToolbarEntity> objSecToolbarEntityList)
        {
            return DataAccess.Update<SecToolbarEntity>(objSecToolbarEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSecToolbarEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSecToolbar(SecToolbarEntity objSecToolbarEntity)
        {
            return DataAccess.Delete(objSecToolbarEntity);
        }

        public static bool DeleteSecToolbar(List<SecToolbarEntity> objSecToolbarEntityList)
        {
            return DataAccess.Delete<SecToolbarEntity>(objSecToolbarEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSecToolbarAll()
        {
            return DataAccess.SelectAllDataSet<SecToolbarEntity>();
        }
        #endregion

        #region Functions
        public static bool SaveToolbars(string mainMenuID, List<SecToolbarEntity> list)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    //Delete
                    string sql = "DELETE FROM SEC_TOOLBAR WHERE MAIN_MENU = '" + mainMenuID + "'";
                    broker.ExecuteSQL(sql);
                    //Add
                    DataAccess.Insert<SecToolbarEntity>(list, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }


}

